import { NextRequest, NextResponse } from "next/server";
import prisma from "../../../../../lib/prisma";

/**
 * 更新聊天标题
 */
export async function PUT(
  request: NextRequest,
  { params }: { params: Promise<{ chatId: string }> }
) {
  try {
    const { chatId } = await params;
    const body = await request.json();
    const { newTitle } = body;

    if (!newTitle || typeof newTitle !== "string") {
      return NextResponse.json({ error: "Invalid title" }, { status: 400 });
    }

    // 更新数据库中的 chat 标题
    const updatedChat = await prisma.chat.update({
      where: {
        id: chatId,
      },
      data: {
        chatTitle: newTitle,
      },
    });

    return NextResponse.json({ chat: updatedChat });
  } catch (error) {
    console.error("Error updating chat title:", error);
    return NextResponse.json(
      { error: "Internal server error" },
      { status: 500 }
    );
  }
}
